package com.hsy.swu.hsyutils

import android.app.Dialog
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Context.CLIPBOARD_SERVICE
import android.graphics.Color
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.hsy.swu.databinding.LayoutLoadingBinding
import com.kongzue.dialogx.dialogs.PopTip
import com.kongzue.dialogx.util.TextInfo

object MActivityUtil {
    fun showToast(
        message: String,
        isLong: Boolean = false,
    ) {
        PopTip.build()
            .setMessage(message)
            .setBackgroundColor(Color.parseColor("#3A3A3A"))
            .setMessageTextInfo(TextInfo().apply {
                fontColor = Color.WHITE
            }).apply {
                if (isLong)
                    showLong()
                else
                    showShort()
            }
    }

    fun showLoading(context: Context): Dialog {
        val loadingDialog = object : HsyDialog<LayoutLoadingBinding>(context, { _, _ -> }, false) {}
        loadingDialog.setCancelable(false)
        loadingDialog.show()
        return loadingDialog
    }

    fun scaleAnimOnShow(view: View, toAlpha: Float = 1f, onAnimEnd: () -> Unit = {}) {
        view.animate()
            .scaleX(1f)
            .scaleY(1f)
            .alpha(toAlpha)
            .setDuration(300)
            .setInterpolator(DecelerateInterpolator())
            .withEndAction { onAnimEnd() }
            .start()
    }

    fun scaleAnimOnDismiss(view: View, onAnimEnd: () -> Unit = {}) {
        view.animate()
            .scaleX(0f)
            .scaleY(0f)
            .setDuration(300)
            .setInterpolator(DecelerateInterpolator())
            .withEndAction { onAnimEnd() }
            .start()
    }

    fun alphaAnimOnShow(view: View, onAnimEnd: () -> Unit = {}) {
        view.animate()
            .alpha(1f)
            .setDuration(300)
            .setInterpolator(DecelerateInterpolator())
            .withEndAction { onAnimEnd() }
            .start()
    }

    fun alphaAnimOnDismiss(view: View, onAnimEnd: () -> Unit = {}) {
        view.animate()
            .alpha(0f)
            .setDuration(300)
            .setInterpolator(DecelerateInterpolator())
            .withEndAction { onAnimEnd() }
            .start()
    }

    fun alphaAnimVisibleLottieEmpty(lottie: LottieAnimationView) {
        lottie.apply {
            visibility = View.VISIBLE
            alphaAnimOnShow(this) {
                playAnimation()
            }
        }
    }

    fun alphaAnimGoneLottieEmpty(lottie: LottieAnimationView) {
        lottie.apply {
            alphaAnimOnDismiss(this) {
                visibility = View.GONE
                cancelAnimation()
                progress = 0f
            }
        }
    }

    fun translationAnimOnSlideUp(view: View, onAnimEnd: () -> Unit = {}) {
        view.animate()
            .translationY(-view.height.toFloat())
            .setDuration(300)
            .setInterpolator(DecelerateInterpolator())
            .withEndAction { onAnimEnd() }
            .start()
    }

    fun translationAnimOnSlideDown(view: View, onAnimEnd: () -> Unit = {}) {
        view.animate()
            .translationY(0f)
            .setDuration(300)
            .setInterpolator(DecelerateInterpolator())
            .withEndAction { onAnimEnd() }
            .start()
    }

    fun clipText(context: Context, textView: TextView, toastTipMsg: String = "已复制到剪贴板") {
        val clipboard = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val text = textView.text.toString().trim()
        val clip = ClipData.newPlainText("text", text)
        clipboard.setPrimaryClip(clip)
        showToast(toastTipMsg)
    }

    fun clipText(context: Context, toClipText: String, toastTipMsg: String = "已复制到剪贴板") {
        val clipboard = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val text = toClipText.trim()
        val clip = ClipData.newPlainText("text", text)
        clipboard.setPrimaryClip(clip)
        showToast(toastTipMsg)
    }
}